package com.jpress.ejb;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.jpress.model.EnqueteOpcao;
import com.jpress.remote.EnqueteOpcaoRemote;

@Stateless(mappedName="bean/enqueteOpcao")
public class EnqueteOpcaoEJB implements EnqueteOpcaoRemote{

	@PersistenceContext(unitName="jpressEE")
	private EntityManager entityManager;

	@Override
	public void inserir(EnqueteOpcao opcao) throws Exception {
		try {
			entityManager.persist(opcao);
		} catch (Exception e) {
			throw e;
		}	
	}

	@Override
	public void atualizar(EnqueteOpcao opcao) throws Exception {
		try {
			entityManager.merge(opcao);
		} catch (Exception e) {
			throw e;
		}	
	}

	@Override
	public void excluir(EnqueteOpcao opcao) throws Exception {
		try {
			opcao = entityManager.merge(opcao);
			entityManager.remove(opcao);
		} catch (Exception e) {
			throw e;
		}	
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<EnqueteOpcao> findAll() {
		Query query = entityManager.createNamedQuery("EnqueteOpcao.findAll");
		return (List<EnqueteOpcao>) query.getResultList();
	}

	@Override
	public EnqueteOpcao findByCodigo(int codigo) {
		Query query = entityManager.createNamedQuery("EnqueteOpcao.findByCodigo");
		query.setParameter("codigo", codigo);
		@SuppressWarnings("unchecked")
		List<EnqueteOpcao> resultado = (List<EnqueteOpcao>) query.getResultList();
		if(resultado.size() == 1)
			return resultado.get(0);
		return null;
	}	
	
}
